﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using PrgsMgr;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using DalMgr;
namespace DataHandle
{
    public class Connection
    {
        private const string DataFileName = "ConData.dat";
        private const string SFT_CFG_CON_SHT_NAME = "CON_SHT_NAME";
        private string ShtName;
        private Dictionary<string,ItemInfo> DicItem;
        public Connection()
        {
            ShtName = SftDataMgr.ReadStr(PubVal.SFT_CFG, SFT_CFG_CON_SHT_NAME, "对应关系");
        }
        public void InitDic()
        {
            DicItem = new Dictionary<string, ItemInfo>();
            
            using(FileStream FS = new FileStream(DataFileName,FileMode.OpenOrCreate,FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(FS,Encoding.Default))
                {
                    string str;
                    string[] arr;
                    while (null != (str = sr.ReadLine()))
                    {
                        arr = str.Split(',');
                        if(arr.Length == 3)
                        {
                            ItemInfo info = new ItemInfo();
                            info.Name = arr[0];
                            info.Department2 = arr[1];
                            info.Department3 = arr[2];
                            if (!DicItem.ContainsKey(info.Name))
                            {
                                DicItem.Add(info.Name, info);
                            }
                        }
                    }
                    sr.Close();
                    FS.Close();
                }
            }
        }
        public void ImportCpnData(object filePath)
        {
            int PrgsID = 0;
            ProgressManager.Init(PrgsID, "", 1);
            if (Funcs.FileIsInUse(filePath.ToString()))
            {
                ProgressManager.UpdateVale(PrgsID, "文件被占用，请先关闭文件");
                ProgressManager.CloseProcessFrm();
                return;
            }
            List<ItemInfo> LstItem = new List<ItemInfo>();
            ProgressManager.UpdateVale(PrgsID, "正在打开文件");
            using (FileStream FS = new FileStream(filePath.ToString(), FileMode.Open, FileAccess.Read))
            {
                IWorkbook wkBk = new XSSFWorkbook(FS);
                ISheet wkSht = wkBk.GetSheet(ShtName);
                if (wkSht == null)
                {
                    wkBk.Close();
                    FS.Close();
                    ProgressManager.UpdateVale(PrgsID,string.Format("所选文件不存在名为【{0}】的工作表", ShtName),true);
                    ProgressManager.CloseProcessFrm();
                    return;
                }
                IRow row;
                ProgressManager.Init(PrgsID, "正在导入对应关系", wkSht.LastRowNum - wkSht.FirstRowNum);
                for (int rowIdx = wkSht.FirstRowNum + 1; rowIdx <= wkSht.LastRowNum; rowIdx++)
                {
                    ProgressManager.UpdateVale(PrgsID);
                    row = wkSht.GetRow(rowIdx);
                    if (row != null && row.LastCellNum < 6)
                    {
                        continue;
                    }
                    ItemInfo info = new ItemInfo();
                    info.Name = row.GetCell(3).StringCellValue;
                    info.Department2 = row.GetCell(4).StringCellValue;
                    info.Department3 = row.GetCell(5).StringCellValue;
                    if (info.Name != "" && info.Department2 != "" && info.Department3 != "")
                    {
                        LstItem.Add(info);
                    }
                }
                wkBk.Close();
                FS.Close();
            }
            ProgressManager.Init(PrgsID, "正在保存对应关系", LstItem.Count);
            using (FileStream FS = new FileStream(DataFileName, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(FS, Encoding.Default))
                {

                    for (int idx = 0; idx < LstItem.Count; idx++)
                    {
                        sw.WriteLine(LstItem[idx].ToString());
                        ProgressManager.UpdateVale(PrgsID);
                    }
                    sw.Close();
                    FS.Close();
                }
            }
            ProgressManager.CloseProcessFrm();
        }

        public bool ItemExsit(string strName)
        {
            return DicItem.ContainsKey(strName);
        }
        public ItemInfo GetItem(string strName)
        {
            return DicItem[strName];
        }

    }
}
